package com.xzuf.nc;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.xzuf.pub.GlobalValue;

/**
 * �ļ���Ի���
 * @author xzgf
 * @since 2008-9-27
 * @version 1.0
 */
public class SchemeSelectionDialog extends JDialog implements ActionListener{

	private JButton btn_ok = new JButton("确定");
	private JButton btn_cancel = new JButton("取消");
	private JComboBox schemeList = new JComboBox();
	private PareValueVO selectedVO = null;
	
	public SchemeSelectionDialog(Frame parent) {
		super(parent);
		initialize();
	}
	
	private void initialize() {
		setTitle("选择方案");
		setSize(new Dimension(300,100));
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);
		setModal(true);
		
		DefaultComboBoxModel listModel = new DefaultComboBoxModel();
		PareValueVO[] vos = getAllScheme();
		if(vos != null && vos.length > 0) {
			for(int i=0;i<vos.length;i++) {
				listModel.addElement(vos[i]);	
			}
			
		}
		schemeList.setModel(listModel);
		JLabel lb_scheme = new JLabel("方案列表");
		lb_scheme.setBounds(10, 10, 60, 22);
		schemeList.setBounds(80, 10, 180, 22);
		btn_ok.setBounds(10, 42, 80, 22);
		btn_ok.addActionListener(this);
		btn_cancel.setBounds(100, 42, 80, 22);
		btn_cancel.addActionListener(this);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.add(lb_scheme);
		panel.add(schemeList);
		panel.add(btn_ok);
		panel.add(btn_cancel);
		
		setContentPane(panel);
	}
	
	public PareValueVO getSelectedVO() {
		return selectedVO;
	}
	
	private void onBoOk() {
		selectedVO = (PareValueVO)schemeList.getSelectedItem();
		this.dispose();
	}
	
	private void onBoCancel() {
		this.dispose();
	}
	
	private PareValueVO[] getAllScheme() {
		File files = new File(GlobalValue.Templet_DIR);
		
		File[] allFiles = files.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				if(name.endsWith(".db")) {
					return true;
				}
				return false;
			}
		});
		
		PareValueVO[] vos = new PareValueVO[allFiles.length];
		for(int i=0;i<allFiles.length;i++) {
			vos[i] = new PareValueVO();
			vos[i].setKey(allFiles[i].getName());
			vos[i].setValue(allFiles[i].getAbsoluteFile());
		}
		return vos;
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btn_ok) {
			onBoOk();
		}
		if(e.getSource() == btn_cancel) {
			onBoCancel();
		}
	}
	
}
